/*******************************************************************************
 * Copyright (c) 2007 TOPCASED. All rights reserved. This program
 * and the accompanying materials are made available under the terms of the
 * Eclipse Public License v1.0 which accompanies this distribution, and is
 * available at http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors: Topcased contributors and others - initial API and implementation
 *******************************************************************************/
/**
 * This is a fork of the UML2Java Plug-in developed by Topcased and released under
 * the Eclipse EPL License. http://www.eclipse.org/legal/epl-v10.html
 * In accordance with the license, I have forked the UML2Java plugin and have adapted
 * it to suit the needs of a UML2QoS instrumentation code generator for PHP.
 * 
 * @author Alexandru Burdusel
 */
package org.topcased.uml2qos;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.net.URISyntaxException;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.ui.actions.WorkspaceModifyOperation;
import org.topcased.uml2qos.log.Uml2QosLog;
import fr.obeo.acceleo.gen.IGenFilter;
import fr.obeo.acceleo.gen.template.eval.LaunchManager;
import fr.obeo.acceleo.chain.impl.spec.CChain;

/**
 * Class that is used for launching the Acceleo Chain.
 * @author Pascal GARCIA (Sopra Group - Toulouse)
 * @author Ariel RAMPHUL (Sopra Group - Toulouse)
 */
public class GenQosOperation extends WorkspaceModifyOperation {

    private static int launchID = 0;
    
    private IGenFilter genFilter = new IGenFilter() 
    { 
        public boolean filter(java.io.File script, IFile targetFile, EObject object) throws CoreException 
        { 
            return true; 
        }
    }; 

    private CChain chain;

    /**
     * Constructor of the class GenQosOperation
     * @param mychain that will be executed
     */    
    public GenQosOperation(CChain mychain) {
        chain = mychain;
    }
    /**
     * Method that will execute the chain and delete the tempory files needed to launch the chain
     * @param monitor is used to update the progress monitor
     */
    protected void execute(IProgressMonitor monitor) throws CoreException, InvocationTargetException, InterruptedException 
    {
        Uml2QosLog.log(1, "UML2QoS - Starting UML2QoS Plugin... ");
        launchID++;
        
        //Generate code
        chain.launch(genFilter, monitor, LaunchManager.create("run", true));
        Uml2QosLog.log(1, "UML2QoS - Code generation completed !");
        
        //Copy the framework in the generated folder - requires exploded jars.
        try {
			FrameworkGenerator.copyFramework();
		} catch (URISyntaxException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
       
        //Clean the tempory data
        IWorkspaceRoot myWorkspaceRoot = ResourcesPlugin.getWorkspace().getRoot();
        IProject myproject = myWorkspaceRoot.getProject(".uml2qos");
        File tmpComments = new File("_tmp");
        myproject.delete(true, monitor);
        GenQos.getmFile().delete(0,monitor);
        tmpComments.delete();
        myWorkspaceRoot.refreshLocal(2, monitor);
    }
    
    /**
     * Return a unique ID at each generation (required for the cleaning mechanism)
     * @return the launchID
     */
    public static int getLaunchID()
    {
        return launchID;
    }

}
